package com.decent.core.alarm.process.action;

import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.json.JSONUtil;
import com.decent.common.bo.MessageBean;
import com.decent.common.enums.alarm.AlarmChannelEnum;
import com.decent.core.alarm.entity.bo.AlarmBO;
import com.decent.core.alarm.model.AlarmModel;
import com.decent.core.alarm.process.AlarmBusiness;
import com.decent.core.alarm.process.AlarmProcessContext;
import com.decent.core.alarm.util.DingDingNoticeRequest;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

/**
 * 发送报警
 *
 * @author menghx
 * @date 2022-3-29
 */
@Slf4j
public class AlarmAction implements AlarmBusiness {

    /**
     * 发送报警
     *
     * @param context 上下文
     */
    @Override
    public void process(AlarmProcessContext context) {
        AlarmModel sendModel = context.getProcessModel();
        AlarmBO alarmBO = sendModel.getAlarmBO();
        if (Objects.equals(alarmBO.getSendChannel(), AlarmChannelEnum.DING_TALK)) {
            try {
                DingDingNoticeRequest.send(alarmBO.getUrl(), alarmBO.getContentPrefix(), alarmBO.getContent());
            } catch (Exception e) {
                log.warn("发送报警,请求异常[{}]", JSONUtil.toJsonStr(context), e);
                context.setNeedBreak(true).setResponse(MessageBean.fail(ExceptionUtil.getSimpleMessage(e)));
            }
            return;
        }
        log.warn("发送报警,未知发送渠道[{}]", JSONUtil.toJsonStr(context));

    }

}
